package com.example.xjttoboxuegu.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.xjttoboxuegu.R;
import com.example.xjttoboxuegu.view.MyInfoView;

public class SettingActivity extends AppCompatActivity {

    private TextView tv_back, tv_main_title;
    private RelativeLayout rl_title_bar, rl_modify_psw, rl_security_setting, rl_exit_login;
    public static SettingActivity instance = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        //设置竖屏
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        instance=this;
        init();
    }

    /**
     * 获取界面控件
     */
    private void init() {
        tv_main_title = findViewById(R.id.tv_main_title);
        tv_main_title.setText("设置");
        tv_back = findViewById(R.id.tv_back);
        rl_title_bar = findViewById(R.id.title_bar);
        rl_title_bar.setBackgroundColor(Color.parseColor("#30B4FF"));
        rl_modify_psw = findViewById(R.id.rl_modify_psw);
        rl_security_setting = findViewById(R.id.rl_security_setting);
        rl_exit_login = findViewById(R.id.rl_exit_login);
        tv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SettingActivity.this.finish();
            }
        });

        //修改密码点击事件
        rl_modify_psw = findViewById(R.id.rl_modify_psw);
        rl_modify_psw.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //跳转到密码修改界面
                Intent intent = new Intent(SettingActivity.this,ModifyPswActivity.class);
                startActivity(intent);
            }
        });

        //密保点击事件
        rl_security_setting = findViewById(R.id.rl_security_setting);
        rl_security_setting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //跳转到密保设置界面
                Intent intent = new Intent(SettingActivity.this, FindPswActivity.class);
                intent.putExtra("from","security");
                startActivity(intent);
            }
        });

        //推出登录点击事件
        rl_exit_login = findViewById(R.id.rl_exit_login);
        rl_exit_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(SettingActivity.this, "退出登录成功", Toast.LENGTH_SHORT).show();

                //清除SharedPreferences中的登录状态和登录时的用户名
                clearLoginStatus();

                //将退出登录成功的状态传递给MainActivity
                Intent intent = new Intent();
                intent.putExtra("isLogin",false);
                setResult(RESULT_OK,intent);
                SettingActivity.this.finish();
            }
        });
    }

    /**
     * 清除SharedPreferences中的登录状态和登录时的用户名
     */
    private void clearLoginStatus() {
        SharedPreferences sp = getSharedPreferences("loginInfo",MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putBoolean("isLogin",false);
        editor.putString("loginUserName","");
        editor.commit();
    }
}